package com.bstu.ciit.lab4;

import java.util.Random;

public class Population {
	private int count=2; // The number of chromosomes in the population
	private int size=64;
	private int game[];
	private int gameSize;
	private int money[];
	private int chromosoms [][]; // Chromosomes array (population)
	public int getGameSize() {
		return gameSize;
	}
	public void gameCalc()
	{
		int begin=(int)(Math.log(size)/Math.log(2));
		Random r=new Random();
		for(int i=0;i<begin;i++)
			game[i]=r.nextInt(2);
		for(int i=begin;i<gameSize*2;i++)
		{
			int b=0;
			for(int j=begin-1;j>=0;j--)
				b+=(int) (game[i-j-1]*Math.pow(2,j));
			if(i%2==0)
				game[i]=chromosoms[0][b];
				else
					game[i]=chromosoms[1][b];
		}
	}
	public int[] getGame() {
		return game;
	}
	public int[] getMoney() {
		return money;
	}
	public int[][] getChromosoms() {
		return chromosoms;
	}
	public void setChromosoms(int[][] chromosoms) {
		this.chromosoms = chromosoms;
	}
	public int getCount() {
		return count;
	}
	public int getSize() {
		return size;
	}
	public Population (int size, int gameSize)
	{
		Random random = new Random();
		this.gameSize=gameSize;
		this.size=size;
		this.chromosoms=new int[this.count][this.size];
		//Random filling
		for (int i=0; i<count; i++)
			for (int j=0; j<size; j++)
					this.chromosoms[i][j]=random.nextInt(2);	
		this.money=new int[count];
		this.game=new int[2*gameSize];
	}
	/*
	 * Print this population
	 */
	public void print()
	{
		System.out.println("CHROMOSOM:");
		for(int i=0;i<count;i++)
		{
			for(int j=0;j<size;j++)
				System.out.print(chromosoms[i][j]+" ");
			System.out.println();
		}
	}
	public void manyCalc()
	{
		for(int i=0;i<2;i++)
			money[i]=0;
		for(int i=0;i<gameSize*2;i+=2)
		{
			if(game[i]==game[i+1] && game[i]==0)
			{
				money[0]+=1;
				money[1]+=1;
			}
			else if(game[i]==game[i+1] && game[i]==1)
			{
					money[0]+=3;
					money[1]+=3;
			}
			else if (game[i]>game[i+1])
			{
					money[0]+=5;
					money[1]+=0;
			}
			else
			{
				money[0]+=0;
				money[1]+=5;
			}
		}
	}
	public void printGame()
	{
		int mas[][]=new int[2][gameSize];
		int k=0;
		for(int i=0;i<gameSize;i++)
			for(int j=0;j<2;j++)
			{
				mas[j][i]=game[k];
				k++;
			}
		System.out.println("GAME:");
		for(int i=0;i<2;i++)
		{
			for(int j=0;j<gameSize;j++)
				System.out.print(mas[i][j]+"  ");
			System.out.println("Result money="+money[i]);
		}
	}
}
